package com.itboye.ihomebank.activity.keytwo;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.presenter.KeyTwoPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.sitri.sdk.SLock;
import com.sitri.sdk.callback.visible.CommonCallback;
import com.sitri.sdk.model.ID;
import com.sitri.sdk.model.ResultData;
import com.sitri.sdk.model.ResultError;

import java.util.Observable;
import java.util.Observer;

import static com.itboye.ihomebank.util.SPUtils.get;

/**
 * 锁-设置
 */
public class KeySetUpActivity extends BaseOtherActivity implements Observer, CommonCallback {
    View v_statusbar;
    private ImageView close_icon;// 返回图片
    private TextView add_shap_title_tv;// 标题
    KeyTwoPresenter keyTwoPresenter;
    TextView save;
    ToggleButton shezhi_one_tb;
    private String alias;
    private String lockId;
    private String mac;
    private String uid;
    EditText edit_name;
    TextView tv_tongbu;//同步锁时间

    @Override
    protected int layout_id() {
        return R.layout.activity_key_set_up;
    }

    @Override
    protected void onInitController() {

    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        add_shap_title_tv.setText("设置");
        save.setVisibility(View.VISIBLE);
        keyTwoPresenter = new KeyTwoPresenter(this);
        uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
        lockId = getIntent().getStringExtra("lockid");
        alias = getIntent().getStringExtra("lockAlias");
        mac = getIntent().getStringExtra("mac");

        SLock.getServices().setCommonCallback(this);
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        SLock.getServices().disconnectBle();
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.close_icon:
                finish();
                break;
            case R.id.tv_tongbu:
                showProgressDialog("正在同步。。。。。。", false);
                SLock.getServices().timeSync(mac, System.currentTimeMillis());
                break;
            case R.id.shezhi_one_tb:
                showProgressDialog("稍后。。。。。。", true);
                if (shezhi_one_tb.isChecked()) {
                    keyTwoPresenter.onTuiSong(uid, lockId, 1);
                } else {
                    keyTwoPresenter.onTuiSong(uid, lockId, 0);
                }
                break;
            case R.id.save:

                if (alias.equals(edit_name.getText().toString())) {
                    ByAlert.alert("当前没有做修改");
                    return;
                }
                alias = edit_name.getText().toString();
                if (alias.equals("")) {
                    ByAlert.alert("请输入钥匙名字");
                    return;
                }
                showProgressDialog("正在修改，请稍后......", false);
                keyTwoPresenter.onUpdateKey(uid, lockId, alias);
                break;
            default:
                break;
        }

    }

    @Override
    public void update(Observable o, Object data) {
        // TODO Auto-generated method stub
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == KeyTwoPresenter.updateKey_success) {
                ByAlert.alert(entity.getData());
                Intent intent2 = new Intent("changeavart");
                sendBroadcast(intent2);
                finish();
            } else if (entity.getEventType() == KeyTwoPresenter.updateKey_fail) {
                ByAlert.alert(entity.getData());
            } else if (entity.getEventType() == KeyTwoPresenter.tuisong_success) {
                ByAlert.alert(entity.getData());
            } else if (entity.getEventType() == KeyTwoPresenter.tuisong_fail) {
                ByAlert.alert(entity.getData());
            }
        }
    }

    @Override
    public void onSuccess(ResultData resultData) {
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
        if (resultData.getId() == ID.TIME_SYNC) {//开锁成功之后返回的开锁记录
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ByAlert.alert("同步成功");
                }
            });
        }
    }

    @Override
    public void onFailed(ResultError resultError) {
        try {
            closeProgressDialog();
        } catch (Exception e) {
        }
    }
}
